package com.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.constant.AuthConstant;
import com.utils.RedisClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.domain.entity.ChongdianzhuangbaoxiuEntity;

import com.service.ChongdianzhuangbaoxiuService;
import com.utils.PageUtils;
import com.utils.R;

@RestController
@RequestMapping("/chongdianzhuangbaoxiu")
public class ChongdianzhuangbaoxiuController {
    @Autowired
    private ChongdianzhuangbaoxiuService chongdianzhuangbaoxiuService;

    @Autowired
    private RedisClient redisClient;


    /**
     * 报修分页查询
     */
    @GetMapping("/page")
    public R page(@RequestParam int pageNum,@RequestParam int pageSize,ChongdianzhuangbaoxiuEntity chongdianzhuangbaoxiu){

		PageUtils page = chongdianzhuangbaoxiuService.queryPage(pageNum,pageSize,chongdianzhuangbaoxiu);

        return R.ok().put("data", page);
    }

    /**
     * 报修信息更新
     */
    @PostMapping("/update")
    public R update(@RequestBody ChongdianzhuangbaoxiuEntity chongdianzhuangbaoxiu, HttpServletRequest request){
        chongdianzhuangbaoxiuService.updateById(chongdianzhuangbaoxiu);//全部更新
        return R.ok();
    }


    /**
     * 报修信息删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        chongdianzhuangbaoxiuService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 保修信息详情
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        String json = redisClient.getString(AuthConstant.RESTORED_INFO_PREFIX + id);
        if (StringUtils.isNotEmpty(json)) {
            ChongdianzhuangbaoxiuEntity entity = JSONObject.parseObject(json, ChongdianzhuangbaoxiuEntity.class);
            return R.ok().put("data", entity);
        }

        ChongdianzhuangbaoxiuEntity entity = chongdianzhuangbaoxiuService.selectById(id);
        if (entity != null) {
            redisClient.setMinString(AuthConstant.REPORT_INFO_PREFIX + id,JSONObject.toJSONString(entity),2000L);
        }

        return R.ok().put("data", entity);
    }
}
